﻿@using ImapX
@using PagedList
@{
    var folder = ViewBag.CurrentFolder as Folder;
    ViewBag.Title = (folder == null ? "" : folder.Name + " - ") + "ImapX";
    var messages = ViewBag.Messages as PagedList<Message>;
}
@if (folder == null)
{
    <p class="no-data">
        Please select a folder to display the list of messages
    </p>
}
else
{
    <h1>@folder.Name</h1>

    <ul class="message-list">
        @foreach (var msg in messages)
        {
            <li>
                <a href="javascript:;" data-id="@msg.MessageId.Trim('<','>')" data-subject="@(string.IsNullOrWhiteSpace(msg.Subject) ? "No subject" : msg.Subject)">
                    <div class="sender">
                        @(string.IsNullOrWhiteSpace(msg.From.DisplayName) ? msg.From.Address : msg.From.DisplayName)
                    </div>
                    <div class="subject">
                        @if (msg.Attachments.Any())
                        {
                            <span class="icon icon-paperclip"></span>
                        }
                        @(string.IsNullOrWhiteSpace(msg.Subject) ? "No subject" : msg.Subject)
                    </div>
                    <div class="date">
                        @msg.Date.Value.ToString("g")
                    </div>
                </a>
            </li>
        }
    </ul>

    <ul class="paging">
        @for (var i = 1; i <= messages.PageCount; i++)
        {
            <li @Html.Raw(messages.PageNumber == i ? "class=\"active\"" : "")>
                <a href="@(messages.PageNumber == i ? "javascript:;":(ViewBag.IsInbox == true ? Url.Action("index", "home", new {page = i}) : Url.Action("index", "home", new {folder = folder.Path, page = i})))">
                    <span class="icon icon-stop"></span>
                </a>
            </li>
        }
    </ul>
    <div class="clear"></div>
}
@section scripts {
    <script>
        $(function () {
            var paging = $('.paging');
            paging.css('margin-left', ((paging.parent().width() - paging.width()) / 2) - 30 + 'px');

            var reader = $('.reader');

            $('.message-list a').click(function () {
                var self = $(this);
                reader.find('header h2').text(self.data('subject'));
                reader.find('iframe')[0].src = '@Url.Action("message", "home",new {folder=folder.Path})&id=' + self.data('id');
            });
        });
    </script>
}